package com.jxau.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jxau.entity.view.IndexArticles;
import com.jxau.service.ArticleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:applicationContext.xml")
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    private ObjectMapper objectMapper = new ObjectMapper();
    @GetMapping("/queryAllArticles")
    public void queryAllArticles(HttpServletResponse response) throws IOException {
        ArrayList<IndexArticles> articles = articleService.queryAllArticles();
        String articlesJson = objectMapper.writeValueAsString(articles);
        System.out.println(articlesJson);
        response.getWriter().print(articlesJson);
    }
}
